package me.zhengjie.modules.his.recordsCenter.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.his.domain.Doctor;
import me.zhengjie.modules.his.recordsCenter.service.DoctorService;
import me.zhengjie.modules.his.recordsCenter.service.dto.DoctorQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author his
* @date 2022-11-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "his:医生表管理")
@RequestMapping("/api/doctor")
public class DoctorController {

    private final DoctorService doctorService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('doctor:list')")
    public void exportDoctor(HttpServletResponse response, DoctorQueryCriteria criteria) throws IOException {
        doctorService.download(doctorService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询his:医生表")
    @ApiOperation("查询his:医生表")
    @PreAuthorize("@el.check('doctor:list')")
    public ResponseEntity<Object> queryDoctor(DoctorQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(doctorService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增his:医生表")
    @ApiOperation("新增his:医生表")
    @PreAuthorize("@el.check('doctor:add')")
    public ResponseEntity<Object> createDoctor(@Validated @RequestBody Doctor resources){
        return new ResponseEntity<>(doctorService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改his:医生表")
    @ApiOperation("修改his:医生表")
    @PreAuthorize("@el.check('doctor:edit')")
    public ResponseEntity<Object> updateDoctor(@Validated @RequestBody Doctor resources){
        doctorService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除his:医生表")
    @ApiOperation("删除his:医生表")
    @PreAuthorize("@el.check('doctor:del')")
    public ResponseEntity<Object> deleteDoctor(@RequestBody Integer[] ids) {
        doctorService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
